package pl.edu.pw.polygen.modeler.server.xml;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class PolygenData {
	private List<Vertex> vertexes = new ArrayList<Vertex>();

	@XmlElementWrapper(name = "vertexes")
	@XmlElement(name = "vertex")
	public List<Vertex> getVertexes() {
		return vertexes;
	}

	public void setVertexes(List<Vertex> vertexes) {
		this.vertexes = vertexes;
	}
	
	
	public List<Vertex> getVertexes(String type){
		List<Vertex> result = new ArrayList<Vertex>();
		for(Vertex v : vertexes){
			if(type.equals(v.getType())){
				result.add(v);
			}
		}
		return result;
	}
	
	
	public List<Edge> getEdges(){
		List<Edge> result = new ArrayList<Edge>();
		for(Vertex v : vertexes){
			for(Edge e : v.getEdges()){
				if("SEGMENT".equals(e.getType()))
					result.add(e);
			}
		}
		return result;
	}
	
	
	
	@Override
	public String toString(){
		StringBuilder stringBuilder = new StringBuilder();
		for(Vertex v : vertexes){
			stringBuilder.append(v.toString()+ "\n");
		}
		return stringBuilder.toString();
	}

	public List<Edge> getSplains() {
		List<Edge> result = new ArrayList<Edge>();
		for(Vertex v : vertexes){
			for(Edge e : v.getEdges()){
				if("SPLAIN".equals(e.getType()))
					result.add(e);
			}
		}
		return result;
	}
	
	public Vertex getVertexById(int id){
		for(Vertex v : vertexes){
			if(v.getId()==id){
				return v;
			}
		}
		return null;
	}

	public int getLastId() {
		int max = 0;
		for(Vertex v : vertexes){
			if("NORMAL_VERTEX".equals(v.getType())){
				if(v.getId() > max)
					max = v.getId();
			}
		}
		return max;
	}
	
	public Set<Integer> getBoundaryIndicators(){
		Set<Integer> boundarySet = new HashSet<Integer>();
		for(Vertex v : vertexes){
			if("NORMAL_VERTEX".equals(v.getType())){
				if(v.getBoundary()!=0)
					boundarySet.add(v.getBoundary());
			}
		}
		return boundarySet;
	}

}
